package com.pf.java;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static java.util.Collections.unmodifiableList;

/**
 * Collections 针对操作集合的工具类
 *
 * @author pf
 * @data 2021/11/2-14:47
 */
public class collections_tool {

    public static void main(String[] args) {
        // 排序
        List scores = new ArrayList();
        scores.add(12);
        scores.add(13);
        scores.add(14);
        scores.add(15);
        scores.add(16);
        scores.add(22);
        System.out.println(scores);

        Collections.shuffle(scores);//打乱
        System.out.println(scores);

        Collections.sort(scores);
        System.out.println(scores);

        //Collections.reverse(scores);//颠倒
        System.out.println(scores);

        // 查找，替换
        // 对Collection有效
        Object max = Collections.max(scores);
        System.out.println(max);

        Object min = Collections.min(scores);
        System.out.println(min);

        // List
        int index = Collections.binarySearch(scores, 22);
        System.out.println("索引：" + index);// 找索引

        Collections.replaceAll(scores, 14, 41);
        System.out.println(scores);

        // 不可变集合 数据不可改动
        List list = Collections.emptyList();
        System.out.println(list);
//        list.add(100);

        Set set = Collections.singleton(200);
        System.out.println(set);
//        set.add(250);

        List scoreList = Collections.unmodifiableList(scores);
        System.out.println(scoreList);
//        scoreList.add(100);
    }

}
